package com.briup.cms.service.impl;

import com.briup.cms.bean.Article;
import com.briup.cms.bean.extend.ArticleExtend;
import com.briup.cms.dao.ArticleMapper;
import com.briup.cms.dao.extend.ArticleExtendMapper;
import com.briup.cms.dao.extend.CommentExtendMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IArticleService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleExtendMapper articleExtendMapper;
    @Autowired
    private CommentExtendMapper commentExtendMapper;

    @Override
    public Article selectByPrimaryKey(Integer id) throws CustomerException {
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article == null){
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
        }
        return article;
    }

    @Override
    public Article saveOrUpdate(Article article) {
        if (article.getArticleId() != null){
            articleMapper.updateByPrimaryKey(article);
            return article;
        }
        article.setArticlePublishTime(new Date());
        article.setArticleReadTimes(0);
        article.setArticleStatus(0);
        article.setArticleThumpUp(0);
        articleMapper.insert(article);
        return article;
    }

    @Override
    public void changeStatus(Integer id, Integer status) throws CustomerException{
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article == null){
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
        }
        //修改查询到的article状态，更新
        article.setArticleStatus(status);
        articleMapper.updateByPrimaryKey(article);
    }

    @Override
    public void delete(Integer id) throws CustomerException{
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article == null){
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_ARTICLE_NOT_EXIST);
        }
        commentExtendMapper.deleteCommentArticleId(id);
        articleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ArticleExtend> pageQueryArticleWithCategoryAndUser(Integer num, Integer size, String tittle, Integer status, Integer categoryId) throws CustomerException {
        PageHelper.startPage(num,size,true);
        List<ArticleExtend> list = articleExtendMapper.queryArticleWithCategoryAndUser(tittle,status,categoryId);
        if(list == null){
            throw new CustomerException(MessageStatus.DATA_NONE);
        }
        PageInfo<ArticleExtend> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public PageInfo<ArticleExtend> pageQueryArticleRecommended(int num, int size) {
        PageHelper.startPage(num, size, true);
        List<ArticleExtend> list = articleExtendMapper.queryArticleRecommended();
        if(list == null){
            throw new CustomerException(MessageStatus.DATA_NONE);
        }
        return new PageInfo(list);
    }

    @Override
    public void deleteArticleByCategoryId(int categoryId) throws CustomerException {
        List<ArticleExtend> list = articleExtendMapper.queryArticleWithCategoryAndUser(null,null,Integer.valueOf(categoryId));
        for (ArticleExtend i : list){
            commentExtendMapper.deleteCommentArticleId(i.getArticleId());
        }
        articleExtendMapper.deleteArticleByCategoryId(categoryId);
    }
}
